package Invalid;
/*
 *  Remote Keyboard Project
 *  Server Side
 *  Supervised by Professor Gustavo Rodriguez-Rivera.
 *  Memeber: Yuxi Chen, Xinpi Du, Deen Liu, Zhongjun Jin
 *  Last Modified: Zhongjun Jin
 *  Modified Date: 09/19/2012
 * 
 * 	Date:09/21/2012
 * 	Modified: Deen Liu
 * 	Detail: Basically, the ServerControl class has been modified as a Thread to receive command from client. 
 * 			(client send command as out.print() now)
 * 
 */
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.Robot;
import java.lang.*;


public class PresentationHelperServer {
	
	private ServerSocket serverSocket = null;
	private InetAddress currentIp;
	private ServerControl myControl;
	private String password;
	
	// Current mode: 0 is mouse, 1 is key, 2 is SlideShow.
	int mode;
	
	public PresentationHelperServer( int port , String pass )
	{
		// Save the password of the current server.
		this.password = pass;
		// Get current IP address and show it in the screen.
		getIp();
		// Create server control
		//myControl = new ServerControl();
		
		listenTo( port );
		runServer();

	}
	
	private void listenTo( int port )
	{
		// Listen to a certain
		try 
		{
            this.serverSocket = new ServerSocket(port);
        } 
		catch (IOException e) 
		{
        	System.err.println("Could not listen on port: " + port );
            System.exit(1);
        }
		System.out.println("Successfully listen on port: " + port );
	}
	private void getIp()
	{
		try 
		{
			currentIp = InetAddress.getLocalHost();
			System.out.println("Current IP address : " + currentIp.getHostAddress());
		} 
		catch (UnknownHostException e) 
		{
 			e.printStackTrace();
 		}
	}
	
	private void runServer()  {
		System.out.println("Server runs successfully!");
		Socket clientSocket = null;
		try
		{
			clientSocket = serverSocket.accept();	
		}
		catch( Exception e)
		{
			System.err.println("Can not hear from a client");
		}
		if( clientSocket != null )
		{
			 System.out.println( "A clinent is connected!" );
		}
		try{
        	InputStream inStream = clientSocket.getInputStream();
        	OutputStream outStream = clientSocket.getOutputStream();
        	Scanner in = new Scanner( inStream );
        	PrintWriter out =  new PrintWriter(outStream, true );
        	this.myControl = new ServerControl(inStream, outStream);
        	Thread controlThread = new Thread(this.myControl);
        	controlThread.start();
	        while( clientSocket != null )
	        {
	        	Thread.sleep( 1000 );
	        	out.println("Hello world!");
	        }
		}
		catch(Exception e)
		{
			
		}
	}
	
	public static void main (String[] args) throws Exception 
	{
		System.out.print("Create a password:");
		Scanner in = new Scanner( System.in );
		String newPass = in.nextLine();
		System.out.print("Choose a port I will listen to:");
		int port = in.nextInt();
		SSLServer newServer = new SSLServer( port , newPass );
    }
}
